package com.example.ylgh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.ylgh.model.hosp.Hospital;
import com.example.ylgh.repository.HospitalRepository;
import com.example.ylgh.service.HospitalService;
import com.example.ylgh.vo.hosp.HospitalQueryVo;
import com.example.yygh.cmn.client.DictFeignClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hx
 * @create 2022/5/11 10:00
 * description:
 */
@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;
    @Autowired
    private DictFeignClient dictFeignClient;
    @Override
    public void save(Map<String, Object> map) {
        //把参数map集合转换为对象 Hospital
        String mapString = JSONObject.toJSONString(map);
        Hospital hospital = JSONObject.parseObject(mapString,Hospital.class);
        //判断是否存在数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist =hospitalRepository.getHospitalByHoscode(hoscode);
        //如果存在，进行修改
        if (hospitalExist !=null){
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else {
            //如果不存在，进行添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    /**
     * 带条件查询医院的分页
     * @param limit
     * @param page
     * @param hospitalQueryVo
     * @return
     */
    @Override
    public Page<Hospital> selectHosp(Integer limit, Integer page, HospitalQueryVo hospitalQueryVo) {
        //将hospitalQueryVo转换为hospital
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        //创建条件匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        //创建example对象
        Example<Hospital> example = Example.of(hospital, matcher);
        //创建pageable对象
        Pageable pageable = PageRequest.of(page-1, limit);
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

        pages.getContent().stream().forEach(item->{
            this.setHospitalType(item);
        });
        return pages;
    }

    /**
     * 更新医院上线状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(String id, Integer status) {
        //根据id查询医院信息
        Hospital hospital = hospitalRepository.findById(id).get();
        //设置修改的值
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    /**
     * 医院详情接口
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> getHospById(String id) {
        Map<String, Object> map = new HashMap<>();
        //根据id查询医院信息
        Hospital hospital = hospitalRepository.findById(id).get();
        //封装全部信息 等级信息啥的
        Hospital hospitalAll = setHospitalType(hospital);
        map.put("hospital",hospitalAll);
        //单独处理更直观
        map.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return map;
    }

    //根据医院id获取医院名称
    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospital!=null){
            return hospital.getHosname();
        }
        return null;
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String, Object> result = new HashMap<>();
        //医院详情
        Hospital hospital = this.setHospitalType(this.getByHoscode(hoscode));
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }


    private Hospital setHospitalType(Hospital hospital) {
        //根据dictCode和value获取医院名称
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
        //查询省市区
        String ProvinceName = dictFeignClient.getName(hospital.getProvinceCode());
        String CityName = dictFeignClient.getName(hospital.getCityCode());
        String DistrictName = dictFeignClient.getName(hospital.getDistrictCode());
        hospital.getParam().put("fullAddress",ProvinceName+CityName+DistrictName);
        hospital.getParam().put("hostypeString",hostypeString);
        return hospital;
    }
}
